#include <iostream>
#include <fstream>

#include "run_test.h"

#include "function.h"
#include "ZFunction.h"
#include "B2_function.h"
#include "griewank_function.h"
#include "rosenbrock_function.h"
#include "shekel_function.h"
#include "zakharov_function.h"

void test_CGM(int argc, char *argv[ ])
{
  char File1[80]; // used for ZFunction only
  char File2[80]; // used for ZFunction only
  char OutputDir[80];   // Used to temp store the output directory
  int seed;
  double ftol;
  int nruns; // number of runs for each temperature
  int ndim; // number of degrees of freedom

  if (argc == 8)
  {
    // required content of input arguments
    //
    // argv[1]     seed
    // argv[2]     ftol
    // argv[3]    N_runs 
    // argv[4]    the number of dimensions
    // argv[5]     zmatrix
    // argv[6]     inf data file

    seed = atoi(argv[1]);
    ftol = atof(argv[2]);
    nruns = atoi(argv[3]);
    ndim = atoi(argv[4]);
    strcpy_s(File1, 80, argv[5]);
    strcpy_s(File2, 80, argv[6]);
    strcpy_s(OutputDir, 80, argv[7]);
  }
  
  else if (argc == 1)
  {
    ifstream file("input/params_cgm.txt");

    if (! file)
    {
      cout << "Cannot open params.in\n";
      exit(1);
    }

    file >> seed >> ftol >> nruns >> ndim >> File1 >> File2 >> OutputDir;

    file.close();
  }
  else
  {
    cout << "Incorrect input.\n";
    exit(1);
  }

  string dir(OutputDir);

  cout << "-------------------" << endl
       << "|       CGM       |" << endl
       << "-------------------" << endl;
  
  Function *Zfunc = new ZFunction(ndim, File1, File2);
  //Function *b2Func = new B2Function(N);
  //Function *grieFunc = new GriewankFunction(N);
  //Function *rosenFunc = new RosenbrockFunction(numDim);
  //Function *shekelFunc = new ShekelFunction(numDimShekel);
  //Function *zakFunc = new ZakharovFunction(numDim);

  cout << "Dimension: " << ndim << endl << endl;

  run_CGM(argc, argv, seed, ftol, Zfunc, nruns, ndim, string("Output/ZfuncMolecule/" + dir + "/"));
  //run_CGM(argc, argv, seed, ftol, Zfunc, nruns, ndim, string("output/ZfuncMolecule/Famot/"));
  //run_CGM(argc, argv, b2Func, string("output/B2/"), numDim);
  //run_CGM(argc, argv, grieFunc, string("output/Griewank/"), N);
  //run_CGM(argc, argv, rosenFunc, string("output/Rosenbrock/"), numDim);
  //run_CGM(argc, argv, shekelFunc, string("output/Shekel/"), numDimShekel);
  //run_CGM(argc, argv, zakFunc, string("output/Zakharov/"), numDim);
}